using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Ltf.MonitorLizard.Controls.BufferedListView
{
	public class BufferedListView: ListView
	{
		public BufferedListView()
			: base()
		{
			DoubleBuffered = true;
			OwnerDraw = true;
		}

		protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
		{
			base.OnDrawColumnHeader(e);
			e.DrawBackground();
			e.DrawText();
		}

		protected override void OnDrawItem(DrawListViewItemEventArgs e)
		{
		//	base.OnDrawItem(e);

	//		e.DrawBackground();
	//		e.DrawFocusRectangle();
			
		}

		protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
		{
			
		//	base.OnDrawSubItem(e);
		//	e.DrawBackground();
			System.Drawing.Rectangle r = e.Bounds;
			System.Drawing.Rectangle r2 = e.Bounds;
			r.X = r.X + 1;
			r.Y = r.Y + 1;
			r.Width = r.Width ;
			r.Height = r.Height ;
			e.Graphics.SetClip(r);
			System.Drawing.Brush b = System.Drawing.Brushes.White;
			if (e.Item.Selected)
			{
				b = System.Drawing.Brushes.LightBlue;
			}
			System.Drawing.Rectangle fillRect = r;
			fillRect.Width += 7;
			e.Graphics.FillRectangle(b, new System.Drawing.Rectangle(new System.Drawing.Point(e.Bounds.X, e.Bounds.Y), new System.Drawing.Size(fillRect.Width, fillRect.Height)));
			

			TextFormatFlags tffm = TextFormatFlags.Left | TextFormatFlags.WordEllipsis;
			System.Drawing.Rectangle textRect = r;
			if (e.ColumnIndex == 0)
			{
				textRect.X += 16;
				textRect.Width -= 16;
			}
			textRect.Height = 15;
			TextRenderer.DrawText(e.Graphics, e.SubItem.Text, this.Font, textRect, this.ForeColor, (e.Item.Selected) ? System.Drawing.Color.LightBlue : System.Drawing.Color.White, tffm);


			if (e.ColumnIndex == 0 && SmallImageList!=null)
			{
				e.Graphics.DrawImage(SmallImageList.Images[e.Item.ImageKey], r.Location);
			}
			
		}
	}
}
